<?php
namespace app\admin\service;

use app\common\model\ProjectModel;

class ProjectService
{
    private static $projectModel;

    /**
     * 依赖注册
     * ProjectService constructor.
     * @param ProjectModel $projectModel
     */
    public function __construct(ProjectModel $projectModel)
    {
        self::$projectModel = $projectModel;
    }

    /**
     * 显示充电桩检测项目
     * User: zhouyao
     * Date: 2018/9/8
     * Time: 下午2:11
     * @param $param
     * @return array
     * @throws \app\common\exception\AppException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($param)
    {
        if (empty($param) || !isset($param['page']) || !isset($param['limit'])) {
            app_fail(9997); // 参数异常
        }

        $where = ['is_del' => 1];
        // 获取总数
        $count = self::$projectModel->where($where)->count();
        if ($count < 1) {
            return ['data' => [], 'count' => $count];
        }
        // 查询管理员列表
        $result = self::$projectModel->where($where)
            ->where('pid', '=', 0)
            ->page($param['page'])
            ->limit($param['limit'])
            ->order('id', 'asc')
            ->select();

        foreach ($result as $key => $val) {
            $val->_child = self::$projectModel->where('pid', '=', $val->id)->where('is_del', '=', 1)->select();
        }


        return ['data' => $result, 'count' => $count];
    }

    /**
     * 添加充电桩检测项目
     * User: zhouyao
     * Date: 2018/9/8
     * Time: 下午1:46
     * @param $param
     * @return bool
     * @throws \app\common\exception\AppException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function store($param)
    {
        if (empty($param) || !isset($param['project_name'])) {
            app_fail(9997);
        }

        // 检查充电桩是否存在
        $row = self::$projectModel->where('project_name', '=', $param['project_name'])->find();
        if (!empty($row)) {
            app_fail(9994);
        }

        try {
            self::$projectModel->allowField(true)->save($param);
            return true;
        } catch (\Exception $e) {
            app_fail(9998);
        }
    }

    /**
     * 查询指定充电桩项目信息
     * User: zhouyao
     * Date: 2018/9/8
     * Time: 下午1:54
     * @param $param
     * @return array|bool|null|\PDOStatement|string|\think\Model
     * @throws \app\common\exception\AppException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit($param)
    {
        if (empty($param) || !isset($param['id'])) {
            app_fail(9997);
        }

        $row = self::$projectModel->where('id', '=', $param['id'])->find();
        return $row ? $row : false;
    }

    /**
     * 修改充电桩项目信息
     * User: zhouyao
     * Date: 2018/9/8
     * Time: 下午1:58
     * @param $param
     * @return bool
     * @throws \app\common\exception\AppException
     */
    public function update($param)
    {
        if (empty($param) || !isset($param['id'])) {
            app_fail(9997);
        }

        // 检测当前类型下面是否有子级
        $row = self::$projectModel->where('pid', '=', $param['id'])->find();
        if (!empty($row)) {
            app_fail(9993);
        }

        try {
            self::$projectModel->allowField(true)->save($param, ['id' => $param['id']]);
            return true;
        } catch (\Exception $e) {
            app_fail(9998);
        }

    }

    /**
     * 获取所有顶级类型
     * User: zhouyao
     * Date: 2018/9/9
     * Time: 下午10:00
     * @param $param
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cate($param)
    {
        $data = self::$projectModel->where('pid', '=', 0)
            ->where('is_del', '=', 1)
            ->order('id', 'asc')
            ->field(['id', 'project_name'])
            ->select();
        $data->unshift(['project_name' => '检测项目类型', 'id' => 0]);
        return $data;
    }

}